#ifndef _DYNARRAY_H_
#define _DYNARRAY_H_

#include<iostream>
#include<exception>

template<typename type>
class DynArray
{
private:
	type *arr;
	int arrSize, arrCapacity;

private:
	//disable the stuffs

	DynArray(type &item){}
	DynArray& operator=(DynArray &item){ return *this;}
public:

	//constructor
	explicit DynArray(unsigned int initialSizeAndCap = 0)
	{
		if(initialSizeAndCap > 0)
			arr = new type[initialSizeAndCap];
		arrSize = initialSizeAndCap;
		arrCapacity = initialSizeAndCap;
	}

	//destructor
	virtual ~DynArray(void)
	{
		clear();
	}

	//zero based index subscript operator
	type& operator[](const int index)
	{
		try
		{
			if(index < 0 || index >= capacity())
			{
				throw exception("Invalid index\n");
			}
		return arr[index];

		}
		catch(std::exception arg)
		{
			
			std::cout << arg.what();
		}

		type bla;
		return bla;
	}

	//return the size of the array
	int size(void)
	{
		return arrSize;
	}

	void append(const type &item)
	{
		if(arrCapacity == 0)
		{
			arr = new type;
			++arrCapacity;
		}

		if( arrSize == arrCapacity)
		{
			arrCapacity*=2;
			type *temp = new type[arrCapacity];

			for(int i=0; i < size();++i)
			{
				temp[i] = arr[i];
			}
			if(arrCapacity == 1)
				delete arr;
			else
				delete [] arr;
			arr = temp;
		}

		arr[arrSize] = item;
		++arrSize;
	}

	int capacity(void)
	{
		return arrCapacity;
	}

	void clear(void)
	{
		if(arrCapacity == 1)
			delete arr;
		else
			delete [] arr;

		arrCapacity = 0;
		arrSize = 0;
	}
};

#endif /*_DYNARRAY_H_*/
